﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BussinessLayer;
using DataAccessLayer;
using System.Text.RegularExpressions;
using System.Media;

namespace QLBaoHanh.TiepNhan
{
    public partial class frmThietBiBHNhan : Form
    {

        private bool _isEditForm;
        private Phien_ThietBiBH _phien_ThietBiBH;
        private int _phien_ThietBiBHId;
        private frmPhieuNhan _callerForm;

        public int Phien_ThietBiBHId
        {
            set
            {
                _phien_ThietBiBHId = value;
                //reload LoaiDichVu
                _phien_ThietBiBH = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById(_phien_ThietBiBHId);
                RefreshForm();
            }
        }

        public bool IsEditForm
        {
            set
            {
                _isEditForm = value;
                if (!_isEditForm)
                    FillForm();
            }
            get
            {
                return _isEditForm;
            }
        }

        public frmPhieuNhan CallerForm
        {
            get { return _callerForm; }
            set { _callerForm = value; }
        }


        private void RefreshForm()
        {
            FillForm();
        }

        private void FillForm()
        {
            // Fill combobox nha cung cap
            cboNhaCungCap.DataSource = NhaCungCapBO.getInstance.GetAllNhaCungCap();
            cboNhaCungCap.DisplayMember = "Ten";
            cboNhaCungCap.ValueMember = "Id";
            // Fill combobox nha san xuat
            cboNhaSanXuat.DataSource = NhaSanXuatBO.getInstance.GetAllNhaSanXuat();
            cboNhaSanXuat.DisplayMember = "Ten";
            cboNhaSanXuat.ValueMember = "Id";

            //Fill combobox loai dich vu
            cboLoaiDichVu.DataSource = LoaiDichVuBO.getInstance.GetAllLoaiDichVu();
            cboLoaiDichVu.DisplayMember = "Ten";
            cboLoaiDichVu.ValueMember = "Id";


            if (_isEditForm)
            {
                ThietBiBaoHanh thietbi = ThietBiBaoHanhBO.getInstance.GetThietBiBaoHanhById(_phien_ThietBiBH.ThietBiBHId);
                txtMa.Text = _phien_ThietBiBH.MaThietBiBaoHanh;
                txtTen.Text = _phien_ThietBiBH.TenThietBiBaoHanh;
                txtSoLuong.Text = _phien_ThietBiBH.SoLuong.ToString();
                txtSerial.Text = _phien_ThietBiBH.Serial;
                txtModel.Text = _phien_ThietBiBH.Model;
                cboNhaCungCap.Text = thietbi.NhaCungCap.Ten;
                cboNhaSanXuat.Text = thietbi.NhaSanXuat.Ten;
                dtpNgayMua.Value = (DateTime)thietbi.NgayMua;
                txtSoThangBH.Text = thietbi.TGianBH.ToString();
                cboLoaiDichVu.Text = _phien_ThietBiBH.TenLoaiDichVu;
                txtMoTaLoi.Text = _phien_ThietBiBH.Loi;
                txtGhiChu.Text = _phien_ThietBiBH.GhiChu;
            }
            else
            {
                this.Text = "Chi tiết sản phẩm bảo hành - sửa chữa";
                txtMa.Text = ThietBiBaoHanhBO.getInstance.GenerateMaThietBiBaoHanh();
                dtpNgayMua.Value = DateTime.Now.AddMonths(-12);
                txtSoThangBH.Text = "12";
                //cboLoaiDichVu.SelectedIndex = 1;
                chkTimKiem.Checked = false;
            }
        }

        private void ChangeTimKiem()
        {
            //Enable cboTimKiem and disable form them thiet bi bao hanh
            bool isTimKiem = chkTimKiem.Checked;
            cboTimKiem.Enabled = isTimKiem;
            grbThietBi.Enabled = !isTimKiem;
            if (isTimKiem)
            {
                txtMa.Text = "";
                dtpNgayMua.Text = "";
                txtSoThangBH.Text = "";
                FillCboTimKiem();
            }
            else
            {
                FillForm();
            }

        }

        private void FillTBBHDuocChon(int inTBBHId)
        {
            if (inTBBHId == 0) return;
            ThietBiBaoHanh thietbi = ThietBiBaoHanhBO.getInstance.GetThietBiBaoHanhById(inTBBHId);
            txtMa.Text = thietbi.Ma;
            txtTen.Text = thietbi.Ten;
            txtSerial.Text = thietbi.Serial;
            txtModel.Text = thietbi.Model;
            cboNhaCungCap.Text = thietbi.NhaCungCap.Ten;
            cboNhaSanXuat.Text = thietbi.NhaSanXuat.Ten;
            dtpNgayMua.Value = (DateTime)thietbi.NgayMua;
            txtSoThangBH.Text = thietbi.TGianBH.ToString();
        }


        private void FillCboTimKiem()
        {
            List<ThietBiBaoHanh> TbiBHData = ThietBiBaoHanhBO.getInstance.GetAllThietBiBaoHanhOrderByTen().ToList();
            ThietBiBaoHanh BlankTBBH = new ThietBiBaoHanh();
            BlankTBBH.Id = 0;
            BlankTBBH.Ten = "- - - - - - - -";
            TbiBHData.Insert(0, BlankTBBH);
            cboTimKiem.DataSource = TbiBHData;
            cboTimKiem.DisplayMember = "TenThietBiAndMa";
            cboTimKiem.ValueMember = "Id";
            cboTimKiem.SelectedIndex = 0;
        }

        private ThietBiBaoHanh SaveThietBiBaoHanhToDB()
        {
            ThietBiBaoHanh obj;
            if (IsEditForm)
            {
                obj = ThietBiBaoHanhBO.getInstance.GetThietBiBaoHanhById(_phien_ThietBiBH.ThietBiBHId);
            }
            else
            {
                obj = new ThietBiBaoHanh();
            }
            obj.Ma = txtMa.Text;
            obj.Ten = txtTen.Text;
            obj.GhiChu = txtGhiChu.Text;
            obj.Model = txtModel.Text;
            obj.Serial = txtSerial.Text;
            obj.NgayMua = dtpNgayMua.Value;
            obj.TGianBH = Convert.ToInt16(txtSoThangBH.Text);
            obj.NCCId = (int)cboNhaCungCap.SelectedValue;
            obj.NSXId = (int)cboNhaSanXuat.SelectedValue;
            if (IsEditForm)
            {
                ThietBiBaoHanhBO.getInstance.UpdateThietBiBaoHanh(obj);
            }
            else
            {
                ThietBiBaoHanhBO.getInstance.InsertThietBiBaoHanh(obj);
                obj = ThietBiBaoHanhBO.getInstance.GetLastRecord();
            }
            return obj;
        }

        private void SaveThietBiBHToGrid()
        {
            //Main goal: Save ThietBiBaoHanh to database and create new Phien_ThietBiBaoHanh obj
            //Luu vao object Phien_ThietBiBaoHanh roi truyen cho grid (chua luu vao database)
            _phien_ThietBiBH = new Phien_ThietBiBH();
            //cboTimKiem == enable (exist devices) then must save device id else mark to add new
            if(cboTimKiem.Enabled)
            {
                int a = cboTimKiem.FindStringExact(cboTimKiem.Text);
                if (a <= 0)
                {
                    MessageBox.Show("Phải chọn thiết bị bảo hành", "Thiết bị bảo hành", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                _phien_ThietBiBH.ThietBiBHId = (int)cboTimKiem.SelectedValue;
                //Them 1 lan bao hanh cho thiet bi nay

            }
            else
            {
                _phien_ThietBiBH.ThietBiBHId = SaveThietBiBaoHanhToDB().Id;
            }

            _phien_ThietBiBH.MaThietBiBaoHanh = txtMa.Text;
            _phien_ThietBiBH.TenThietBiBaoHanh = txtTen.Text;
            _phien_ThietBiBH.Serial = txtSerial.Text;
            _phien_ThietBiBH.Model = txtModel.Text;
            _phien_ThietBiBH.DichVuId = (int)cboLoaiDichVu.SelectedValue;
            _phien_ThietBiBH.TenLoaiDichVu = cboLoaiDichVu.Text;
            _phien_ThietBiBH.GhiChu = txtGhiChu.Text;
            _phien_ThietBiBH.Loi = txtMoTaLoi.Text;
            _phien_ThietBiBH.NCCId = (int)cboNhaCungCap.SelectedValue;
            
            _phien_ThietBiBH.NSXId = (int)cboNhaSanXuat.SelectedValue;
            
            //Thoi han bao hanh = ngay mua + Tgian BH
            _phien_ThietBiBH.ThoiHanBaoHanh = dtpNgayMua.Value.AddMonths(Convert.ToInt16(txtSoThangBH.Text));
            //_phien_ThietBiBH.LanBaoHanh = txtLanBH.Text;  //TODO: Add this column to databse

            try
            {
                _phien_ThietBiBH.SoLuong = Convert.ToInt16(txtSoLuong.Text);
            }
            catch (System.Exception ex)
            {
                _phien_ThietBiBH.SoLuong = 1;
            }
            _phien_ThietBiBH.ModifyState = RecordState.ADD_STATE;
            //Save Thiet bi bao hanh xuong grid
            _callerForm.AddThietBiBaoHanhData(_phien_ThietBiBH);
        }

        public frmThietBiBHNhan()
        {
            InitializeComponent();
            _isEditForm = false;
            _phien_ThietBiBHId = 0;
            _phien_ThietBiBH = null;
        }

        private void frmThietBiBHNhan_Load(object sender, EventArgs e)
        {
            FillForm();
        }

        private void chkTimKiem_CheckedChanged(object sender, EventArgs e)
        {
            ChangeTimKiem();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            FillForm();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveThietBiBHToGrid();
            this.Close();
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            Regex regex = new Regex("[0-9]|\b");
            bool NotMatch = !(regex.IsMatch(e.KeyChar.ToString()));
            e.Handled = NotMatch;
            if (NotMatch)
            {
                SystemSounds.Beep.Play();
            }
        }

        private void txtSoThangBH_KeyPress(object sender, KeyPressEventArgs e)
        {
            Regex regex = new Regex("[0-9]|\b");
            bool NotMatch = !(regex.IsMatch(e.KeyChar.ToString()));
            e.Handled = NotMatch;
            if (NotMatch)
            {
                SystemSounds.Beep.Play();
            }
        }

        private void cboTimKiem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboTimKiem.SelectedItem == null) return;
            int TBiBHId = 0;
            try
            {
                TBiBHId = Convert.ToInt32(cboTimKiem.SelectedValue);
            }
            catch (System.Exception ex)
            {

            }
            FillTBBHDuocChon(TBiBHId);
        }
    }
}
